﻿using BUGCOME.Infrastructure.Cache;
using SqlSugar.Extensions;

namespace BUGCOME.ServiceCore.Services
{
    /// <summary>
    /// 缓存服务类，提供各种缓存操作的静态方法
    /// 支持用户权限、扫码登录、锁定用户、手机/邮箱验证码等缓存功能
    /// </summary>
    public class CacheService
    {
        // 缓存键常量定义
        private readonly static string CK_verifyScan = "verifyScan_";         // 扫码登录缓存前缀
        private readonly static string CK_phoneSmsCode = "phone_sms_code_";   // 手机验证码缓存前缀
        private readonly static string CK_emailSmsCode = "email_sms_code_";   // 邮箱验证码缓存前缀
        private readonly static string CK_forgetPwd = "forgetPwd_";           // 忘记密码缓存前缀

        #region 用户权限 缓存
        /// <summary>
        /// 获取用户权限缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>权限列表</returns>
        public static List<string> GetUserPerms(string key)
        {
            return (List<string>)CacheHelper.GetCache(key);
            //return RedisServer.Cache.Get<List<string>>(key).ToList();
        }

        /// <summary>
        /// 设置用户权限缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="data">权限数据</param>
        public static void SetUserPerms(string key, object data)
        {
            CacheHelper.SetCache(key, data);
            //RedisServer.Cache.Set(key, data);
        }

        /// <summary>
        /// 移除用户权限缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        public static void RemoveUserPerms(string key)
        {
            CacheHelper.Remove(key);
            //RedisServer.Cache.Del(key);
        }
        #endregion

        /// <summary>
        /// 设置扫码登录缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <param name="val">登录信息字典</param>
        /// <returns>缓存操作结果</returns>
        public static object SetScanLogin(string key, Dictionary<string, object> val)
        {
            var ck = CK_verifyScan + key;

            return CacheHelper.SetCache(ck, val, 1); // 默认1分钟过期
        }

        /// <summary>
        /// 获取扫码登录缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <returns>登录信息</returns>
        public static object GetScanLogin(string key)
        {
            var ck = CK_verifyScan + key;
            return CacheHelper.Get(ck);
        }

        /// <summary>
        /// 移除扫码登录缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        public static void RemoveScanLogin(string key)
        {
            var ck = CK_verifyScan + key;
            CacheHelper.Remove(ck);
        }

        /// <summary>
        /// 设置锁定用户缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <param name="val">锁定值</param>
        /// <param name="time">过期时间(分钟)</param>
        public static void SetLockUser(string key, long val, int time)
        {
            var CK = "lock_user_" + key;

            CacheHelper.SetCache(CK, val, time);
        }

        /// <summary>
        /// 获取锁定用户缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <returns>锁定值，0表示未锁定</returns>
        public static long GetLockUser(string key)
        {
            var CK = "lock_user_" + key;

            if (CacheHelper.Get(CK) is long t)
            {
                return t;
            }
            return 0;
        }

        /// <summary>
        /// 缓存手机验证码
        /// </summary>
        /// <param name="key">手机号</param>
        /// <param name="val">验证码</param>
        /// <returns>缓存操作结果</returns>
        public static object SetPhoneCode(string key, string val)
        {
            var ck = CK_phoneSmsCode + key;

            return CacheHelper.SetCache(ck, val, 10); // 默认10分钟过期
        }

        /// <summary>
        /// 校验手机验证码是否正确
        /// </summary>
        /// <param name="key">手机号</param>
        /// <param name="val">验证码</param>
        /// <returns>验证结果</returns>
        public static bool CheckPhoneCode(string key, string val)
        {
            var ck = CK_phoneSmsCode + key;
            var save_code = CacheHelper.Get(ck);

            if (save_code != null && save_code.Equals(val))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 移除手机验证码
        /// </summary>
        /// <param name="key">手机号</param>
        public static void RemovePhoneCode(string key)
        {
            var ck = CK_phoneSmsCode + key;

            CacheHelper.Remove(ck);
        }

        /// <summary>
        /// 缓存邮箱验证码
        /// </summary>
        /// <param name="key">邮箱地址</param>
        /// <param name="val">验证码</param>
        /// <param name="time">过期时间（分钟）默认1分钟</param>
        /// <returns>缓存操作结果</returns>
        public static object SetEmailCode(string key, string val, int time = 1)
        {
            var ck = CK_emailSmsCode + key;

            return CacheHelper.SetCache(ck, val, time);
        }

        /// <summary>
        /// 校验邮箱验证码是否正确
        /// </summary>
        /// <param name="key">邮箱地址</param>
        /// <param name="val">验证码</param>
        /// <returns>验证结果</returns>
        public static bool CheckEmailCode(string key, string val)
        {
            var ck = CK_emailSmsCode + key;
            var save_code = CacheHelper.Get(ck);

            if (save_code != null && save_code.Equals(val))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 缓存邮箱验证码
        /// </summary>
        /// <param name="key">邮箱地址</param>
        public static void RemoveEmailCode(string key)
        {
            var ck = CK_emailSmsCode + key;

            CacheHelper.Remove(ck);
        }

        /// <summary>
        /// 设置忘记密码缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <param name="val">缓存值</param>
        /// <param name="time">过期时间（分钟）默认1分钟</param>
        /// <returns>缓存操作结果</returns>
        public static object SetForgetPwd(string key, string val, int time = 1)
        {
            var ck = CK_forgetPwd + key;

            return CacheHelper.SetCache(ck, val, time);
        }

        /// <summary>
        /// 获取忘记密码缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        /// <returns>缓存值</returns>
        public static string GetForgetPwd(string key)
        {
            var ck = CK_forgetPwd + key;

            return CacheHelper.Get(ck).ObjToString();
        }

        /// <summary>
        /// 移除忘记密码缓存
        /// </summary>
        /// <param name="key">用户标识</param>
        public static void RemovetForgetPwd(string key)
        {
            var ck = CK_forgetPwd + key;

            CacheHelper.Remove(ck);
        }
    }
}